package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	/**
	 * @author perry
	 */
	public class ECGDataLoader extends Sprite
	{
		public static const NAME:String 				 = "ECGDataLoader";
		public static const COMPLETE:String				 = NAME + "Complete";
		
		private var ECGdata:Vector.<Object>;
		private var values:Array;
		private var rawECGData:String;
		
		public function ECGDataLoader()
		{
			
		}
		
		public function init( path:String ):void
		{
			var l:URLLoader = new URLLoader();
			l.addEventListener( Event.COMPLETE, ECGDataCompleteHandler );
			l.load( new URLRequest( path ) );
		}
		
		private function ECGDataCompleteHandler( e:Event ):void
		{
			rawECGData = e.target.data;
			
			rawECGData = UTILS.strReplace(rawECGData, "  ", " ");
			rawECGData = UTILS.strReplace(rawECGData, "	", " ");
			
			values = rawECGData.split("\n");
			
			var subVal:Array;
			ECGdata = new Vector.<Object>();
			
			var i:uint;
			for( i = 2; i < values.length; i++ )
			{
				subVal = values[i].split("  ");
				
				ECGdata.push( {t:subVal[0], mV:subVal[1]} );
			}
			
			dispatchEvent( new Event( COMPLETE ) );
		}
		
		public function get data():Vector.<Object>
		{
			return ECGdata;
		}
		
		public function get rawData():String
		{
			return rawECGData;
		}
	}
}
